﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_AddName : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hidmodie.Value = Request.QueryString["moduid"].ToString();
            lblTime.Text = DateTime.Now.ToShortDateString();
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            lblwz.Text = f.getModuleidAdd(Request["moduid"]);
            initUnit();//单位
            if (Request.QueryString["action"].ToString() == "update")
            {
                hidId.Value = Request.QueryString["SpecificationsId"].ToString();

                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);

                btnSave.Text = " 修改 ";
                initName();
            }
            if (Request.QueryString["action"].ToString() == "add")
            {

                btnSave.Text = " 添加 ";
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                initType();

            }
            if (Request.QueryString["action"].ToString() == "flag")
            {
                hidId.Value = Request.QueryString["SpecificationsId"].ToString();

                btnSave.Text = " 审核 ";
                initName();
            }
        }

    }
    private void initUnit()//单位
    {
        ZSql zUnit = new ZSql();

        zUnit.Open("select * from Units where AuditState=1 order by UnitsOrder");
        ddlUnits.DataSource = zUnit.m_table;
        ddlUnits.DataTextField = "Units";
        ddlUnits.DataValueField = "UnitsId";
        ddlUnits.DataBind();
        zUnit.Close();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //try
        //{
            ZSql zs = new ZSql();

            if (!string.IsNullOrEmpty(hidId.Value))
            {
                int flag = 0;
                if (rbtnFlag.Checked)
                {
                    flag = 1;
                }


                if (Request.QueryString["action"].ToString() == "flag")
                {

                    zs.Open("update Specifications set UserName='" + Server.UrlDecode(Request.Cookies["user"].Values["realname"]) + "',usTime='" + lblTime.Text + "',AuditState='" + flag + "',modulbh='"+Common.FormatParameter(txtbh.Text)+"' where SpecificationsId=" + hidId.Value);
                    Response.Write("<script>window.location='SpecificationsManage.aspx?moduid=" + Request["moduid"] + "';</script>");

                }
                else
                {
                    zs.Open("select * from Specifications where ( Specifications='" + Common.FormatParameter(txtSpf.Text.Trim()) + "' or modulbh='" + Common.FormatParameter(txtbh.Text.Trim()) + "') and SpecificationsId<>" + hidId.Value);
                    if (zs.m_table.Rows.Count > 0)
                    {
                        Response.Write("<script>alert('该型号或者型号编号已存在,请查询后再编辑!');location.href=document.referrer</script>");
                        return;

                    }
                    zs.Open("update Specifications set  Specifications='" + Common.FormatParameter(txtSpf.Text) + "',TypeId=" + ddlType.SelectedValue + ",BrandId=" + ddlBrand.SelectedValue + ",ComoditiesNameId=" + ddlName.SelectedValue + ",SpecificationsOrder='" + Common.FormatParameter(SpfOrder.Text) + "',UserName='" + Server.UrlDecode(Request.Cookies["user"].Values["realname"]) + "',usTime='" + lblTime.Text + "',modulbh='"+Common.FormatParameter(txtbh.Text)+"',Units="+ddlUnits.SelectedValue+" where SpecificationsId=" + hidId.Value);

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                    Response.Write("<script>window.location='SpecificationsManage.aspx?moduid=" + Request["moduid"] + "';</script>");
                }
            }
            else
            {
                if (ddlType.SelectedValue == "0")
                {
                    Response.Write("<script>alert('请选择商品类型');location.href=document.referrer</script>");
                }
                if (ddlBrand.SelectedValue == "0")
                {
                    Response.Write("<script>alert('请选择商品品牌');location.href=document.referrer</script>");
                }
                if (ddlName.SelectedValue == "0")
                {
                    Response.Write("<script>alert('请选择商品名称');location.href=document.referrer</script>");
                }

                zs.Open("select * from Specifications where  Specifications='" + Common.FormatParameter(txtSpf.Text.Trim()) + "' or modulbh='" + Common.FormatParameter(txtbh.Text.Trim()) + "'");
                //Response.Write("select * from Specifications where  Specifications='" + Common.FormatParameter(txtSpf.Text.Trim()) + "' or modulbh='" + Common.FormatParameter(txtbh.Text.Trim()) + "'");
                //Response.End();
                if (zs.m_table.Rows.Count > 0)
                {
                    Response.Write("<script>alert('该型号或者型号编号已存在,请查询后再编辑!');location.href=document.referrer</script>");
                    return;

                }
                else
                {
                    string sql = "insert into Specifications(Specifications,SpecificationsOrder,TypeId,BrandId,ComoditiesNameId,UserName,usTime,modulbh,Units)values('" + Common.FormatParameter(txtSpf.Text.Trim()) + "'," + SpfOrder.Text + "," + ddlType.SelectedValue + "," + ddlBrand.SelectedValue + "," + ddlName.SelectedValue + ",'" + Server.UrlDecode(Request.Cookies["user"].Values["realname"]) + "','" + lblTime.Text + "','" + Common.FormatParameter(txtbh.Text.Trim()) + "'," + ddlUnits.SelectedValue + ")";
                    //Response.Write(sql);
                    //Response.End();
                    zs.Open(sql);
                }
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                Response.Write("<script>window.location='SpecificationsManage.aspx?moduid=" + Request["moduid"] + "';</script>");
            }
            zs.Close();
        //}
       // catch (Exception)
        //{
          //  Response.Write("<script>alert('添加失败');location.href=document.referrer</script>");

           // ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
       // }

    }

    private void initType()
    {
        ZSql type = new ZSql();
        type.Open("select * from ComoditiesType ");
        if (type.m_table.Rows.Count > 0)
        {
            type.Open("select * from ComoditiesType where AuditState=1");
            if (type.m_table.Rows.Count > 0)
            {
                ddlType.DataSource = type.m_table;
                ddlType.DataTextField = "ComoditiesType";
                ddlType.DataValueField = "TypeId";
                ddlType.DataBind();
                type.Close();
                ddlType.Items.Insert(0, new ListItem("—选择商品类型—", "0"));
                ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—", "0"));
                ddlName.Items.Insert(0, new ListItem("—选择商品名称—", "0"));
            }
            else
            {
                Response.Write("<script>alert('类型未审核');</script>");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
        }
        else
        {
            Response.Write("<script>alert('类型为空请添加类型');</script>");
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }

        


    }

    protected void Linkcannel_Click(object sender, EventArgs e)
    {
        Response.Redirect("SpecificationsManage.aspx?moduid=" + hidmodie.Value);
    }

    private void initName()
    {
        if (!string.IsNullOrEmpty(hidId.Value))
        {
            ZSql zs = new ZSql();
            zs.Open("select * from Specifications where SpecificationsId=" + hidId.Value.Trim());
            txtSpf.Text = zs.m_table.Rows[0]["Specifications"].ToString();
            SpfOrder.Text = zs.m_table.Rows[0]["SpecificationsOrder"].ToString();
            //   txtComodid.Text = zs.m_table.Rows[0]["comodid"].ToString();
            hidId1.Value = zs.m_table.Rows[0]["TypeId"].ToString();
            hidId2.Value = zs.m_table.Rows[0]["BrandId"].ToString();
            hidId3.Value = zs.m_table.Rows[0]["ComoditiesNameId"].ToString();
            txtbh.Text = zs.m_table.Rows[0]["modulbh"].ToString();
            ddlUnits.SelectedValue = zs.m_table.Rows[0]["Units"].ToString();
            ZSql t = new ZSql();
            t.Open("select * from ComoditiesType where  AuditState=1 ");

            ddlType.DataSource = t.m_table;
            ddlType.DataTextField = "ComoditiesType";
            ddlType.DataValueField = "TypeId";
            ddlType.DataBind();
            t.Close();
            ddlType.Items.Insert(0, new ListItem("—选择商品类型—", "0"));
            ddlType.SelectedValue = zs.m_table.Rows[0]["TypeId"].ToString();

            ZSql p = new ZSql();
            p.Open("select * from Brand where  TypeId=" + hidId1.Value + " and BrandId=" + hidId2.Value + " and AuditState=1");
            ddlBrand.DataSource = p.m_table;
            ddlBrand.DataTextField = "Brand";
            ddlBrand.DataValueField = "BrandId";
            ddlBrand.DataBind();
            p.Close();
            ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—", "0"));
            ddlBrand.SelectedValue = zs.m_table.Rows[0]["BrandId"].ToString();
            ZSql gg = new ZSql();
            gg.Open("select * from ComoditiesName where BrandId=" + hidId2.Value + " and TypeId=" + hidId1.Value + " and ComoditiesNameId=" + hidId3.Value + "  and AuditState=1");
            ddlName.DataSource = gg.m_table;
            ddlName.DataTextField = "ComoditiesName";
            ddlName.DataValueField = "ComoditiesNameId";
            ddlName.DataBind();
            gg.Close();
            ddlName.Items.Insert(0, new ListItem("—选择商品名称—", "0"));
            ddlName.SelectedValue = zs.m_table.Rows[0]["ComoditiesNameId"].ToString();
            if (zs.m_table.Rows[0]["AuditState"].ToString() == "1")
            {
                rbtnFlag.Checked = true;
                rbtnnotFlag.Checked = false;
            }
            if (zs.m_table.Rows[0]["AuditState"].ToString() == "0")
            {
                rbtnFlag.Checked = false;
                rbtnnotFlag.Checked = true;
            }
        }
    }

    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlType.SelectedIndex > 0)
        {
            ZSql brand = new ZSql();
            brand.Open("select * from Brand where TypeId=" + ddlType.SelectedValue + "");
            if (brand.m_table.Rows.Count > 0)
            {
                brand.Open("select * from Brand where TypeId=" + ddlType.SelectedValue + " and AuditState=1");
                if (brand.m_table.Rows.Count > 0)
                {
                    ddlBrand.DataSource = brand.m_table;
                    ddlBrand.DataTextField = "Brand";
                    ddlBrand.DataValueField = "BrandId";
                    ddlBrand.DataBind();
                    ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—", "0"));
                    brand.Close();
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                }
                else
                {
                    Response.Write("<script>alert('该类品牌未审核');</script>");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                }
            }
            else
            {
                Response.Write("<script>alert('该类品牌为空请添加品牌');</script>");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
        }
        else
        {
            ddlBrand.Items.Clear();
            ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—", "0"));
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }

    }
    protected void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlBrand.SelectedIndex > 0)
        {
            ZSql gg = new ZSql();
            gg.Open("select * from ComoditiesName where BrandId=" + ddlBrand.SelectedValue + "");
            if (gg.m_table.Rows.Count > 0)
            {
                gg.Open("select * from ComoditiesName where BrandId=" + ddlBrand.SelectedValue + " and AuditState=1");
                if (gg.m_table.Rows.Count > 0)
                {
                    ddlName.DataSource = gg.m_table;
                    ddlName.DataTextField = "ComoditiesName";
                    ddlName.DataValueField = "ComoditiesNameId";
                    ddlName.DataBind();
                    ddlName.Items.Insert(0, new ListItem("—选择商品名称—", "0"));
                    gg.Close();
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                }
                else
                {
                    Response.Write("<script>alert('该商品未审核');</script>");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                }
            }
            else
            {
                Response.Write("<script>alert('该商品为空请添加商品');</script>");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
        }
        else
        {
            ddlName.Items.Clear();
            ddlName.Items.Insert(0, new ListItem("—选择商品名称—", "0"));
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }
    }
}
